<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="广告位名称 / 查询" class="handle-input mr10" clearable></el-input>
                <el-button type="primary" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
                <el-button type="danger" class="mr10" icon="el-icon-lx-add" @click="func_handleAdd" plain>新建广告位</el-button>
				<el-dropdown :hide-on-click="false">
                    <el-button class="green mr10" plain>操作</el-button>
					<template #dropdown>
						<el-dropdown-menu>
                            <el-dropdown-item type="text" @click="func_handleBatchDel" class="red "> 批量删除</el-dropdown-item>
                            <el-dropdown-item type="text" @click="func_handleBatchIsActive" class="green "> 启用 </el-dropdown-item>
                            <el-dropdown-item type="text" @click="func_handleBatchIsStop" class="green "> 停用 </el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
            </div>
            <el-table :data="advertTableData" border class="table" header-cell-class-name="table-header" @selection-change="func_handleSelectionChange">
                <el-table-column type="selection" width="55" />
				<el-table-column prop="id" label="ID" width="55" align="center" v-if="false"></el-table-column>
				<el-table-column label="序号" width="80" align="center"><template #default="scope">
						{{scope.$index + 1}}
				</template></el-table-column>
                <el-table-column prop="sdvSno" label="广告位编号" align="left"></el-table-column>
				<el-table-column prop="advName" label="广告位名称" align="left"></el-table-column>
				<el-table-column label="广告展示位置" align="left" >
                    <template #default="scope">
						{{scope.row. advPosition}} - {{scope.row.strAdvPosition}}
				    </template>
                </el-table-column>
                <el-table-column prop="advOpenWay" label="广告展示方式" width="150px" >
                    <template #default="scope">
						{{scope.row. advOpenWay}} - {{scope.row.strAdvOpenWay}}
				    </template>
                </el-table-column>
                <el-table-column prop="isStatus" label="广告位状态" width="150px" >
                    <template #default="scope">
						{{scope.row.isStatus}} - {{scope.row.strIsStatus}}
				    </template>
                </el-table-column>
				<el-table-column prop="createTime" label="创建时间" width="150px" >
                    <template #default="scope">
                        {{scope.row.createTime === null ? 0 : scope.row.createTime }}
                    </template>
				</el-table-column>
                <el-table-column label="操作" :width="optColumnWidth" align="center" >
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-delete" class="red mr10" @click="func_handleDelete(scope.$index, scope.row)">删除</el-button>
                        <el-dropdown :hide-on-click="false">
                            <span class="el-dropdown-link green mr10"><el-icon class="el-icon-lx-info mr10" />更多</span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item type="text" @click="func_handleEdit(scope.$index, scope.row)" class="green"> 编辑 </el-dropdown-item>
                            		<el-dropdown-item type="text" @click="func_changeRowStatus(scope.$index, scope.row, 2001)" class="green"> 启用 </el-dropdown-item>
                            		<el-dropdown-item type="text" @click="func_changeRowStatus(scope.$index, scope.row, 2002)" class="red"> 停用 </el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                    :page-size="query.pageSize" :total="dataTotal" @current-change="func_handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- ---------------------------------------------------------------------------------------------------------------------- -->
        <!-- 编辑弹出框 -->
        <el-dialog title="新建广告位" v-model="vAdvertDialogVisible" width="55%" :close-on-click-modal="false">
            <VAdvertForm v-bind:selectValue="vAdvertData" @advertData="func_loadvAdvertData" />
        </el-dialog>
    </div>
</template>
<script>
import { ref, reactive, toRefs, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { findAdvertListPage, deleteAdvert, batchEditStatus, batchDeleteAdvert, } from "../../../api/hairmanager/advert";
import { NO_DATA_SELECT_ERROR, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE } from "../../../api/CommonErrorMessage.js";
import VAdvertForm from "./components/advertForm.vue";

export default {
    name: "adverts",
    components: {VAdvertForm,},
    setup() {
        const router = useRouter();
		const route = useRoute();
        const optColumnWidth = 150;
        const multipleSelection = ref([])
        //主数据条件查询
        const query = reactive({
            keyword: '',
            pageIndex: 1,
            pageSize: 10,
        });
        // 主数据总页数
        const dataTotal = ref(0);
        // 主数据表格
        let advertTableData = ref([]);
        // =========================================================================================================
        // 主业务处理 ========================================================================================= START
        /**
         * @name 主数据查询操作
         */
        const func_handleSearch = () => {
            query.pageNum = 1;
            func_getData();
        };
        /**
         * @name 分页导航
         */
        const func_handlePageChange = (val) => {
            // query.pageIndex = val;
            query['pageNum'] = val;
            func_getData();
        };
		/**
         * @name 主数据编辑对话框操作
         */
        const func_handleAdd = () => {
			vAdvertData.value['load'] = false;
            vAdvertData.value['id'] = 0;
            vAdvertDialogVisible.value = true;
        };
        /**
         * @name 读取广告位详细数据
         */
        let idx = -1;
        const func_handleEdit = (index, row) => {
            idx = index;
			vAdvertData.value['load'] = true;
            vAdvertData.value['id'] = row['id'];
            vAdvertDialogVisible.value = true;
        };
        /**
         * @name 行内数据状态变更
         */
		const func_changeRowStatus = (index, row, status) => {
			if(index === -1){
				return;
			} else {
				if(index > -1){
					let obj = {'ids':[row['id']], 'status':status};
					batchEditStatus(obj).then((res) => {
						if(res.code != 200) {
							if(res.type == 0) {
								ElMessage.warning(res.data.msg);
							} else {
								ElMessage.error(res.data.msg);
							}
						} else {
							func_getData();
							ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
						}
					});
				}
			}
		};
        /**
         * @name 删除数据操作
         */
        const func_handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {type: "warning",}).then(() => {
                func_del(row['id']);
            }).catch(() => {});
        };
		/**
		 * @name 批量删除数据
		 */
        const func_handleBatchDel = () => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            }
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {type: "warning", }).then(() => {
                func_batchDel(multipleSelection.value);
            }).catch(() => {});
        };
		/**
		 * @name 选择行批量更新数据状态
		 * @desp 批量启用
		 */
		const func_handleBatchIsActive = () => {
			if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            };
            var obj = { ids: multipleSelection.value, status: 1 };
            batchEditStatus(obj).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
		}
		/**
		 * @name 选择行批量更新数据状态
		 * @desp 批量停用
		 */
        const func_handleBatchIsStop = () => {
            if(multipleSelection.value.length ===0){
                ElMessage.warning(NO_DATA_SELECT_ERROR);
                return false;
            };
            var obj = { ids: multipleSelection.value, status: 0 };
            batchEditStatus(obj).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        };
        const func_handleSelectionChange = (val) => {
            let ids = [];
            val.forEach((element) => {
                ids.push(element['id']);
            });
            multipleSelection.value = ids;
        };
        // 主业务处理 ========================================================================================= END
        /**
         * @name 删除广告位配置
         */
        const func_del = (id) => {
            deleteAdvert(id).then((res)=>{
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        };
        /**
         * @name 批量删除广告位配置
         */
        const func_batchDel = (v) => {
            batchDeleteAdvert(obj).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.message);
                    } else {
                        ElMessage.error(res.message);
                    }
                } else {
                    ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        }
        /**
         * @name 获取表格数据
         */
        const func_getData = () => {
            findAdvertListPage(query).then((res) => {
                advertTableData.value = res.data.list;
                dataTotal.value = res.data.total;
            });
        };
        func_getData();
        // 业务插件处理 ========================================================================================= START
        const vAdvertDialogVisible = ref(false);
        const vAdvertData = ref({
            load: false,
            id: 0,
        });
        const func_loadvAdvertData = (v) => {
            if(v['state'] === 1) {
                vAdvertDialogVisible.value = false;
                func_getData();
            } else {
                vAdvertDialogVisible.value = true;
            }
        };
        // 业务插件处理 ========================================================================================= END
        // 业务数据监听
        watch(() => route.query.load, (val) => {
			if(val){
                func_getData();
			}
        },{deep: true,immediate: true});
        // =========================================================================================================
        return {
            // 系统环境变量 =========================================================================================
            optColumnWidth, query, dataTotal, multipleSelection, advertTableData,
            // 主业务处理 =========================================================================================
            func_handleSearch, func_handlePageChange, func_handleAdd, func_handleEdit, func_handleDelete,
            func_changeRowStatus, func_handleBatchDel, func_handleBatchIsActive, func_handleBatchIsStop, func_handleSelectionChange,
            // 数据执行处理 =========================================================================================
            func_getData, func_del, func_batchDel,
			// 业务插件处理 =========================================================================================
			vAdvertDialogVisible, vAdvertData, func_loadvAdvertData,
        };
    },
};
</script>
<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-select {
    width: 120px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.green {
    color: #67c23a;
}
.blue {
    color: #1684FC;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>